<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="SKU名称:">
        <el-input v-model="queryParams.skuName" size="small" placeholder="请输入SKU名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品条码:">
        <el-input v-model="queryParams.sn" size="small" placeholder="请输入商品条码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品牌名称:">
        <el-input v-model="queryParams.brandName" size="small" placeholder="请输入品牌名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类ID:">
        <el-input v-model="queryParams.categoryId" size="small" placeholder="请输入分类ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类名称:">
        <el-input v-model="queryParams.categoryName" size="small" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单价:">
        <el-input v-model="queryParams.supplierPrice" size="small" placeholder="请输入单价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="重量:">
        <el-input v-model="queryParams.weight" size="small" placeholder="请输入重量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="规格:">
        <el-input v-model="queryParams.spec" size="small" placeholder="请输入规格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="库存:">
        <el-input v-model="queryParams.stock" size="small" placeholder="请输入库存" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="预警库存:">
        <el-input v-model="queryParams.warningStock" size="small" placeholder="请输入预警库存" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品图片:">
        <el-input v-model="queryParams.image" size="small" placeholder="请输入商品图片" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品图片列表:">
        <el-input v-model="queryParams.images" size="small" placeholder="请输入商品图片列表" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.spuId" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属店铺ID:">
        <el-input v-model="queryParams.storeId" size="small" placeholder="请输入所属店铺ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.brandId" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商定价单位 :">
        <el-input v-model="queryParams.priceUnit" size="small" placeholder="请输入供应商定价单位 " clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:basicSku:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['goods:basicSku:insert']" type="primary" icon="el-icon-plus" size="mini" :disabled="isSingle" @click="handleAddFromCloud">从云仓选商品添加</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="SKU名称" align="center" prop="skuName" />
      <el-table-column label="商品条码" align="center" prop="sn" />
      <el-table-column label="品牌名称" align="center" prop="brandName" />
      <el-table-column label="分类ID" align="center" prop="categoryId" />
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="单价" align="center" prop="supplierPrice" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="规格" align="center" prop="spec" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="预警库存" align="center" prop="warningStock" />
      <el-table-column label="商品图片" align="center" prop="image" />
      <el-table-column label="商品图片列表" align="center" prop="images" />
      <el-table-column label="" align="center" prop="spuId" />
      <el-table-column label="所属店铺ID" align="center" prop="storeId" />
      <el-table-column label="商品状态 1-正常，2-下架，3-删除" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="brandId" />
      <el-table-column label="供应商定价单位 " align="center" prop="priceUnit" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['goods:basicSku:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['goods:basicSku:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="basicSkuFormDialog.title" :visible.sync="basicSkuFormDialog.visible" :close-on-click-modal="false" :before-close="resetBasicSkuFormDialog" width="500px" append-to-body>
      <el-form ref="basicSkuForm" :model="basicSkuFormData" :rules="basicSkuFormRules" label-width="80px">
        <el-form-item label="SKU名称" prop="skuName">
          <el-input v-model="basicSkuFormData.skuName" placeholder="请输入SKU名称" />
        </el-form-item>
        <el-form-item label="商品条码" prop="sn">
          <el-input v-model="basicSkuFormData.sn" placeholder="请输入商品条码" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brandName">
          <el-input v-model="basicSkuFormData.brandName" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="分类ID" prop="categoryId">
          <el-input v-model="basicSkuFormData.categoryId" placeholder="请输入分类ID" />
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="basicSkuFormData.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="单价" prop="supplierPrice">
          <el-input v-model="basicSkuFormData.supplierPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="basicSkuFormData.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="规格" prop="spec">
          <el-input v-model="basicSkuFormData.spec" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="basicSkuFormData.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="预警库存" prop="warningStock">
          <el-input v-model="basicSkuFormData.warningStock" placeholder="请输入预警库存" />
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <el-input v-model="basicSkuFormData.image" placeholder="请输入商品图片" />
        </el-form-item>
        <el-form-item label="商品图片列表" prop="images">
          <el-input v-model="basicSkuFormData.images" placeholder="请输入商品图片列表" />
        </el-form-item>
        <el-form-item label="" prop="spuId">
          <el-input v-model="basicSkuFormData.spuId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="所属店铺ID" prop="storeId">
          <el-input v-model="basicSkuFormData.storeId" placeholder="请输入所属店铺ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="basicSkuFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="" prop="brandId">
          <el-input v-model="basicSkuFormData.brandId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="供应商定价单位 " prop="priceUnit">
          <el-input v-model="basicSkuFormData.priceUnit" placeholder="请输入供应商定价单位 " />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmBasicSkuFormDialog">确 定</el-button>
        <el-button @click="resetBasicSkuFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BasicSkuApi from '@/api/goods/BasicSkuApi'

const defaultBasicSkuFormData = {
  skuName: null,
  sn: null,
  brandName: null,
  categoryId: null,
  categoryName: null,
  supplierPrice: null,
  weight: null,
  spec: null,
  stock: null,
  warningStock: null,
  image: null,
  images: null,
  spuId: null,
  storeId: null,
  status: '0',
  brandId: null,
  priceUnit: null
}
export default {
  name: 'BasicSkuList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        skuName: null,
        sn: null,
        brandName: null,
        categoryId: null,
        categoryName: null,
        supplierPrice: null,
        weight: null,
        spec: null,
        stock: null,
        warningStock: null,
        image: null,
        images: null,
        spuId: null,
        storeId: null,
        status: null,
        brandId: null,
        priceUnit: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      basicSkuFormDialog: {
        title: '',
        visible: false
      },
      basicSkuFormData: Object.assign({}, defaultBasicSkuFormData),
      basicSkuFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openBasicSkuFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return BasicSkuApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleAddFromCloud() {
      const id =  this.multipleSelection.map(item => item.id)[0]
       this.$router.push('/spu/handleAddFromCloudBySku/'+ id)

    },
    handleQuery() {
      this.loading = true
      BasicSkuApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return BasicSkuApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openBasicSkuFormDialog(id) {
      this.basicSkuFormDialog.visible = true
      if (id) {
        this.basicSkuFormDialog.title = '云仓共享仓储sku-修改'
        BasicSkuApi.get(id).then(response => {
          this.basicSkuFormData = {
            ...this.basicSkuFormData,
            ...response.data
          }
        })
      } else {
        this.basicSkuFormDialog.title = '云仓共享仓储sku-添加'
      }
    },
    confirmBasicSkuFormDialog() {
      this.$refs['basicSkuForm'].validate(valid => {
        if (valid) {
          if (this.basicSkuFormData.id) {
            BasicSkuApi.edit(this.basicSkuFormData).then(response => {
              this.$message.success('修改成功')
              this.resetBasicSkuFormDialog()
              this.handleQuery()
            })
          } else {
            BasicSkuApi.add(this.basicSkuFormData).then(response => {
              this.$message.success('添加成功')
              this.resetBasicSkuFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetBasicSkuFormDialog() {
      this.basicSkuFormDialog = {
        title: '',
        visible: false
      }
      this.basicSkuFormData = Object.assign({}, defaultBasicSkuFormData)
      this.resetForm('basicSkuForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
